﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


public class ComplexNumber
{
    private double real, imaginery;

    public double Imaginery
    {
        get { return imaginery; }
        set { imaginery = value; }
    }

    public double Real
    {
        get { return real; }
        set { real = value; }
    }

    public ComplexNumber(double real, double imaginery)
    {
        this.real = real;
        this.imaginery = imaginery;
    }

    public static ComplexNumber operator +(ComplexNumber first, ComplexNumber second)
    {
        return new ComplexNumber(first.Real + second.Real, first.Imaginery + second.Imaginery);
    }

    public static ComplexNumber operator -(ComplexNumber first, ComplexNumber second)
    {
        return new ComplexNumber(first.Real - second.Real, first.Imaginery - second.Imaginery);
    }

    public static ComplexNumber operator *(ComplexNumber first, ComplexNumber second)
    {
        return new ComplexNumber(first.Real * second.Real - first.Imaginery * second.Imaginery,
            first.Real * second.Imaginery + first.Imaginery * second.Real);
    }

    public static ComplexNumber operator /(ComplexNumber first, ComplexNumber second)
    {
        if (second.Real == 0 && second.Imaginery == 0)
        {
            throw new DivideByZeroException();
        }
        return first * Conjugate(second);
    }

    public static ComplexNumber Conjugate(ComplexNumber first)
    {
        return new ComplexNumber(first.Real, first.Imaginery * (-1));
    }

    public override string ToString()
    {
        String toString = "";
        if (this.Real != 0)
        {
            toString = this.Real.ToString();
            if (this.Imaginery > 0)
            {
                toString += String.Format(" + {0}i", this.Imaginery.ToString());
            }
            if (this.Imaginery < 0)
            {
                toString += String.Format(" - {0}i", (-1 * this.Imaginery).ToString());
            }
        }
        else
        {
            if (this.Imaginery == 0)
            {
                toString = "0";
            }
            else
            {
                toString = String.Format("{0}i", this.Imaginery.ToString());
            }
        }
        return toString;
    }

    public static implicit operator ComplexNumber(double number)
    {
        return new ComplexNumber(number, 0);
    }

    public static explicit operator Double(ComplexNumber number)
    {
        if (number.Imaginery == 0)
        {
            return Double.NaN;
        }
        return number.Real;
    }

    public double this[int index]
    {
        get
        {
            if (index == 0)
            {
                return this.Real;
            }
            if (index == 1)
            {
                return this.Imaginery;
            }
            throw new ArgumentOutOfRangeException();
        }
        set
        {
            if (index == 0)
            {
                this.Real=value;
            }
            if (index == 1)
            {
                this.Imaginery=value;
            }
            throw new ArgumentOutOfRangeException();
        }
    }

    public static void Main()
    {
        ComplexNumber c = new ComplexNumber(0, 5);
        ComplexNumber d = new ComplexNumber(3, 0);
        ComplexNumber e = new ComplexNumber(0, 0);
        Console.WriteLine((double)c);
        Console.WriteLine((double)d);

    }
}

